/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with AdminOverlay.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#include "XMLRPCServerSession.h"
#include "XMLRPCUtils.h"

XMLRPCServerSession::XMLRPCServerSession(LPCTSTR pIP, int pPort, LPCTSTR pPassword)
:ServerSession(pIP, pPort, pPassword) { }

XMLRPCServerSession::~XMLRPCServerSession(void)
{
	Disconnect();
}
void XMLRPCServerSession::Connect()
{
	socket.disconnectFromHost();
	socket.connectToHost(QString::fromUtf16(ip),port);
	if(socket.waitForConnected(5000) != true || 
		socket.state() != QAbstractSocket::ConnectedState)
	{
		throw socket.errorString();
	}	
	if(XMLRPCUtils::authenticate(&socket, QString::fromUtf16(password)) == true)
	{
		IsConnected(true);
	}
	else
	{
		IsConnected(false);
		throw QString("authentication failed");
	}
}
void XMLRPCServerSession::Disconnect()
{
	socket.disconnectFromHost();
	IsConnected(false);
}
void XMLRPCServerSession::Reset()
{
	Disconnect();
	Connect();
}

bool XMLRPCServerSession::TestConnection()
{
	try
	{
		ExecuteCommand(TEXT("sv_port"));
		return true;
	}
	catch (...)
	{
		IsConnected(false);
		return false;
	}
}

LPCTSTR XMLRPCServerSession::ExecuteCommand( LPCTSTR command )
{
	if(IsConnected() == false || socket.state() != QAbstractSocket::ConnectedState)
		throw QString("Socket Not Connected @ExecuteCommand");
	XMLRPCUtils::postCommand(&socket, QString::fromUtf16(command));
	QString result = XMLRPCUtils::getResult(&socket);
	return result.utf16();
}

void XMLRPCServerSession::Say( LPCTSTR textToSay )
{
	ExecuteCommand((QString("sv_say ") + QString::fromUtf16(textToSay)).utf16());
}

LPCTSTR XMLRPCServerSession::GetStatus()
{
	return ExecuteCommand(TEXT("status"));
}

vector<Player*>* XMLRPCServerSession::GetPlayers()
{
	QString status = QString::fromUtf16(GetStatus());
	if (status.contains("Connection Status:") == false)
	{
		//no players
		return new vector<Player*>();
	}
	vector<Player*>* playerList = new vector<Player*>();
	QStringList playerStringList = status.split("Connection Status:", QString::SkipEmptyParts).at(1)
		.split("\n", QString::SkipEmptyParts);
 	for(int i = 0; i < playerStringList.size(); i++)
 	{
		Player *p = XMLRPCUtils::buildPlayer(playerStringList[i]);
 		playerList->push_back(p);
	}
	return playerList;
}

void XMLRPCServerSession::Ban( Player player )
{

}

void XMLRPCServerSession::Kick( Player player )
{

}
